package io.shqyang.done.slice.main;

import io.shqyang.done.ResourceTable;
import io.shqyang.done.slice.main.base.BaseAbilitySlice;
import io.shqyang.done.slice.main.model.Done;
import io.shqyang.done.slice.main.provider.DoneProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.agp.components.TextField;
import ohos.agp.window.dialog.ToastDialog;

import java.util.ArrayList;
import java.util.List;

public class MainAbilitySlice extends BaseAbilitySlice implements Component.ClickedListener {

    private ListContainer listContainer;
    private List<Done> doneList;
    private DoneProvider doneProvider;
    private TextField newDoneTf;

    @Override
    public int layoutId() {
        return ResourceTable.Layout_ability_main;
    }

    @Override
    public void initView() {
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_done_list);
        doneList = new ArrayList<>();
        doneProvider = new DoneProvider(this, doneList);
        listContainer.setItemProvider(doneProvider);
        newDoneTf = (TextField) findComponentById(ResourceTable.Id_newDone_tf);
        findComponentById(ResourceTable.Id_add_btn).setClickedListener(this);
    }

    @Override
    public void loadData(boolean isRefresh) {
        sampleDone("玩游戏", true);
        sampleDone("刷牙", false);
        sampleDone("洗脸", false);
        sampleDone("睡觉", false);
        listContainer.setItemProvider(doneProvider);
    }

    private void sampleDone(String text, boolean isDone) {
        Done done = new Done(1, text, isDone);
        doneList.add(done);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_add_btn:
            {
                String newDoneTitle = newDoneTf.getText();
                if (newDoneTitle.length() == 0) {
                    break;
                }
                Done done = new Done(0, newDoneTitle, false);
                doneList.add(done);
                listContainer.setItemProvider(doneProvider);
                newDoneTf.setText("");
                new ToastDialog(getContext())
                        .setText("已添加新任务")
                        .show();
                break;
            }
        }
    }
}
